#this file is to send a sql group

import util,json
import uuid

restful = 'http://9.181.61.67:8080/cpsrestful1/'
tenantid = 'ccda02c017047c0cfdfafc72'

def constructpublish():
	log={}
	log['name'] = 'dtwtest'
	log['destination'] = 'log'
	log['filename'] = 'dtw.log'
	return log

def constructds():
	ds = {}
	ds['name']='timeseries'
	ds['topicname']= 'dtw'
	ds['type']= 'messagequeue'
	ds['schema_type'] = 'json'
	ds['hostname']= '9.186.89.218'
	ds['port']= 5672
	ds['protocol']= 'amqp'
	ds['username']= 'rollen'
	ds['password']= 'root'
	
	schema = {}
	schema['name'] = 'x'
	schema['datatype'] = 'double'
	
	ds['schema'] = [schema]
	return ds
	
def constructgroup():
	sql = {}
	sql['name'] = 'dtw-test'
	sql['sql'] = 'select data into dtwtest from timeseries group by timewindow(10) having dtw(data, "[1,2,3,4,5]") < 200'
	sql['groupid'] = str(uuid.uuid1()).replace('-','')[:24]
	sql['type'] = 'sql'
	return sql

def constructserv():
	ser = {}
	ser['status'] = 'start'
	return ser
	
def post_an_sql():
	header = {}
	header['Content-Type']='application/json;charset=UTF-8'
	
	##datasource
	data = json.dumps(constructds())
	url = restful + 'DataSources?tenantid=' + tenantid + '&source=rest'
	print url
	util.post(url,data,header)
	
	##publisher
	data = json.dumps(constructpublish())
	url = restful + 'Publishers?tenantid=' + tenantid + '&source=rest'
	print url
	util.post(url,data,header)
	
	##sqlgroup
	data = json.dumps(constructgroup())
	url = restful + 'Group?tenantid=' + tenantid + '&source=sql'	
	print data,url
	util.post(url,data,header)
	
	#data = json.dumps(constructserv())
	#url = restful + 'Service?tenantid=' + tenantid
	#print data,url
	#util.put(url,data,header) 

	
if __name__ == '__main__':
	post_an_sql()
	
